package com.wt.springboot.service.impl;

import com.wt.springboot.bean.Employee;
import com.wt.springboot.mapper.EmployeeMapper;
import com.wt.springboot.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * Created with IDEA
 * Describe:
 * Author:TianJi
 * Date:2018/11/8
 * Time:13:39
 */
@CacheConfig(cacheManager = "employeeCacheManager")
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 将方法的运行结果进行缓存
     *
     *几个属性
     *  cacheNames/value 指定缓存组件的名字
     *  key        缓存数据时用的key,可用它指定，默认是方法参数的值
     *              编写spel #id参数id=#a0 #p0 #root.args[0]
     *  keyGenerator  主键生成器：可以自己指定生成器
     *              key/keyGenerator  二选一
     *  cacheManager  指定缓存管理器
     *  cacheResolver  缓存解析器
     *  condition  指定符合条件的情况下才缓存
     *  unless    否定缓存：当unless指定的条件为true,方法的返回值不缓存，可以获取到结果进行判断
     *
     *  condition = "#id > 0",unless = "#result==null"
     *  sync : 是否使用异步模式
     *
     *  自动配置类:CacheAutoConfiguration
     *      共加载了11个自动缓存配置类，只有一个缓存自动配置类匹配
     *          org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration
     *      将数据保存在concurrentHashMap
     *
     *      使用缓存中间件 redis--redis整合
     *
     *
     * @param id
     * @return
     */

    @Cacheable(cacheNames = {"emp"},condition = "#id > 0",unless = "#result==null")
    @Override
    public Employee getEmployeeById(Integer id) {
        return employeeMapper.getEmployeeById(id);
    }

    @Override
    public void updateEmployee(Employee employee) {
        employeeMapper.updateEmployee(employee);
    }

    @Override
    public void addEmployee(Employee employee) {
        employeeMapper.addEmployee(employee);
    }

    @Override
    public void deleteEmployee(Integer id) {
        employeeMapper.deleteEmployee(id);
    }
}
